package com.vivebest.utils.common.service.impl;

import com.vivebest.utils.common.constant.MsgConstants;
import com.vivebest.utils.common.constant.enums.CmdEnums;
import com.vivebest.utils.common.domain.TransportTemplate;
import com.vivebest.utils.common.dto.CmdResponseDto;
import com.vivebest.utils.common.exception.BizException;
import com.vivebest.utils.common.service.CmdTemplateTransformService;
import com.vivebest.utils.common.utils.CmdUtils;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

/**
 * <p>
 * 命令模板转换服务
 * </p>
 *
 * @author zsk
 * @date 2020/8/12 上午10:43
 */
@Service
@Log4j2
public class CmdTemplateTransformServiceImpl implements CmdTemplateTransformService {

    @Override
    public String transform(TransportTemplate transportTemplate) {
        try {
            //定义StringTemplateLoader
            StringTemplateLoader loader = new StringTemplateLoader();
            loader.putTemplate(transportTemplate.getTemplateName(), transportTemplate.getTemplateContent());

            //定义Configuration
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
            configuration.setTemplateLoader(loader);

            //定义Template
            Template tpl = configuration.getTemplate(transportTemplate.getTemplateName(), "UTF-8");

            StringWriter writer = new StringWriter();
            tpl.process(transportTemplate.getTemplateMap(), writer);
            log.info("命令模板转换结果:{}",writer.toString());
            return writer.toString();
        } catch (Exception e) {
            log.error("命令模板转换异常,模版名称为:{}，异常信息为:{}", transportTemplate.getTemplateName(), e);
            throw new BizException(MsgConstants.COMM100001, transportTemplate.getTemplateName());
        }
    }

    /**
     * 执行命令模板
     *
     * @param cmdCode 命令代码
     * @param argsMap 命令参数
     * @throws IOException 模板转换异常
     */
    @Override
    public CmdResponseDto executeCmdTmp(String cmdCode, Map<String, String> argsMap) throws IOException {
        log.debug("命令模板执行开始===>");

        TransportTemplate transportTemplate = new TransportTemplate();
        transportTemplate.setTemplateContent(CmdEnums.getCmdTmpByCmdCode(cmdCode));
        transportTemplate.setTemplateMap(argsMap);
        transportTemplate.setTemplateName(CmdEnums.getCmdNameByCmdCode(cmdCode));

        String cmd = transform(transportTemplate);
        CmdResponseDto cmdResponseDto = CmdUtils.processCmd(cmd);
        cmdResponseDto.setCmd(cmd);
        log.debug("执行响应结果:{}", cmdResponseDto);
        log.debug("命令模板执行结束<===");
        return cmdResponseDto;
    }

    /**
     * 执行命令模板
     *
     * @param cmdCode 命令代码
     * @param argsMap 命令参数
     * @throws IOException 模板转换异常
     */
    @Override
    public void executeCmdTmpUnWaitFor(String cmdCode, Map<String, String> argsMap) throws IOException {
        log.debug("命令模板执行开始===>");

        TransportTemplate transportTemplate = new TransportTemplate();
        transportTemplate.setTemplateContent(CmdEnums.getCmdTmpByCmdCode(cmdCode));
        transportTemplate.setTemplateMap(argsMap);
        transportTemplate.setTemplateName(CmdEnums.getCmdNameByCmdCode(cmdCode));

        String cmd = transform(transportTemplate);
        CmdUtils.processCmdUnWaitFor(cmd);
        log.debug("命令模板执行结束<===");
    }
}
